package maskedpixel.ogmo.utils 
{
	/**
	 * ...
	 * @author Erin M Gunn
	 */
	public class OgmoLayer
	{
		/**
		 * Describes the tiles Ogmo layer type.
		 */
		public static const TILES:String = "tiles";
		
		/**
		 * Describes the grid Ogmo layer type.
		 */
		public static const GRID:String = "grid";
		
		/**
		 * Describes the objects Ogmo layer type.
		 */
		public static const OBJECTS:String = "objects";
		
		/**
		 * The type of this layer.
		 */
		public var type:String;
		
		/**
		 * The class name that will load this layer, including the class's package.
		 */
		public var cName:String;
		
		/**
		 * The name of this layer.
		 */
		public var name:String;
		
		/**
		 * The size of this layer's grid.
		 */
		public var gridSize:uint;
		
		
		/**
		 * Constructor.
		 * @param	type			Any of the static constants: TILES, GRID, OBJECTS.
		 * @param	cName			The class name that will load this layer, including the class's package.
		 * @param	name			Name of the Layer.
		 * @param	gridSize		Size of the grid to be drawn by OgmoEditor.
		 * @param	...attributes	Any number/combination of OgmoAttribute's, Vector.<OgmoAttribute>'s or
		 * 							Array's of OgmoAttributes.
		 */
		public function OgmoLayer(type:String, cName:String, name:String, gridSize:uint, ...attributes)
		{
			this.cName = cName;
			this.type = type;
			this.name = name;
			this.gridSize = gridSize;
			_attributeList = new Vector.<OgmoAttribute>;
			registerAttribute(attributes);
		}
		
		/**
		 * Generates the XML representing this Layer in the Ogmo Project file.
		 * @return
		 */
		public function generateXML():XML
		{
			var xml:XML = new XML();
			xml = <{type}/>;
			xml.@name = name;
			xml.@gridSize = gridSize;
			for each(var attribute:OgmoAttribute in _attributeList)
			{
				xml.@[attribute.name] = attribute.value;
			}
			return xml;
		}
		
		/**
		 * Registers an Attribute with the project.
		 * @param	...attributes
		 */
		public function registerAttribute(...attributes):void
		{
			for each(var attribute:Object in attributes)
			{
				if (attribute is OgmoAttribute)
				{
					if (_attributeList.indexOf(attribute) == -1)
					{
						_attributeList.push(attribute);
					}
				}
				if (attribute is Vector.<OgmoAttribute> || attribute is Array)
				{
					for each(var att:OgmoAttribute in attribute)
					{
						if (_attributeList.indexOf(att) == -1)
						{
							_attributeList.push(att);
						}
					}
				}
			}
		}
		
		// Attributes
		/** @private */ private var _attributeList:Vector.<OgmoAttribute>;
	}

}